import java.time.LocalDateTime;
import java.util.Scanner;

public class Main {
    private static MovieService movieService = new MovieServiceImpl();
    private static ScheduleService scheduleService = new ScheduleServiceImpl();
    private static TicketService ticketService = new TicketServiceImpl(scheduleService);
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        initData(); // 初始化放映厅 & 演示用数据
        while (true) {
            System.out.println("===== 万东影城管理系统 =====");
            System.out.println("1）登录");
            System.out.println("2）退出系统");
            int cmd = sc.nextInt();
            sc.nextLine();
            if (cmd == 2) {
                break;
            }
            System.out.print("用户名：");
            String uname = sc.nextLine();
            System.out.print("密码：");
            String pwd = sc.nextLine();

            // 简化判断
            if ("admin".equals(uname) && "ynuinfo#777".equals(pwd)) {
                adminMenu();
            } else {
                System.out.println("暂时只支持admin账户演示");
            }
        }
    }

    private static void adminMenu() {
        while (true) {
            System.out.println("----管理员菜单----");
            System.out.println("1）影片管理  2）排片管理  3）售票  4）退票  0）退出登录");
            int c = sc.nextInt();
            sc.nextLine();
            if (c == 0) break;
            switch (c) {
                case 1: movieMenu(); break;
                case 2: scheduleMenu(); break;
                case 3: sellMenu(); break;
                case 4:
                    System.out.print("输入电影票号：");
                    ticketService.refundTicket(sc.nextLine());
                    break;
            }
        }
    }

    private static void movieMenu() {
        System.out.println("1）查看影片  2）添加影片");
        int c = sc.nextInt(); sc.nextLine();
        if (c == 1) {
            for (Movie m : movieService.listMovies()) {
                System.out.println(m);
            }
        } else {
            System.out.println("片名：");String t = sc.nextLine();
            System.out.print("导演：");String d = sc.nextLine();
            System.out.print("主演：");String a = sc.nextLine();
            System.out.print("简介：");String des = sc.nextLine();
            System.out.print("时长：");int dur = sc.nextInt(); sc.nextLine();
            System.out.print("评分：");double s = sc.nextDouble(); sc.nextLine();
            movieService.addMovie(new Movie(t,d,a,des,dur,s));
        }
    }

    private static void scheduleMenu() {
        System.out.println("1）查看排片  2）添加排片");
        int c = sc.nextInt(); sc.nextLine();
        if (c == 1) {
            for (Schedule s : scheduleService.listSchedules()) {
                System.out.println(s);
            }
        } else {
            System.out.print("片名：");
            String name = sc.nextLine();
            Movie movie = movieService.queryMovie(name).get(0);
            Hall hall = new Hall("黄金厅", 8, 10); // demo
            System.out.print("年月日时分(ex:2025-08-20T14:00)：");
            String t = sc.nextLine();
            System.out.print("票价：");
            double p = sc.nextDouble();
            scheduleService.addSchedule(movie,hall, LocalDateTime.parse(t), p);
        }
    }

    private static void sellMenu() {
        System.out.println("场次列表：");
        for (Schedule s : scheduleService.listSchedules()) {
            System.out.println(s);
        }
        System.out.print("输入场次ID：");
        int sid = sc.nextInt();
        System.out.print("排：");
        int r = sc.nextInt();
        System.out.print("列：");
        int c = sc.nextInt(); sc.nextLine();
        ticketService.sellTicket(sid, r, c);
    }

    private static void initData() {
        movieService.addMovie(new Movie("流浪地球2", "郭帆", "吴京", "科幻", 120, 9.0));
        movieService.addMovie(new Movie("封神第一部", "乌尔善", "费翔", "神话史诗", 140, 8.4));
    }
}